﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.MefExtensions;
using System.ComponentModel;
using System.ComponentModel.Composition.Hosting;
using WPLDB.DA.Interfaces.UI;
using System.Windows;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel.Composition;
using WPLDB.DA.Interfaces.UI.Base;
using WPLDB.DA.Models;
using EnvDTE;

namespace WPLDB.Package.UI
{
    public class Bootstrapper : MefBootstrapper, IDisposable
    {

        #region "Override Methods"
        protected override System.Windows.DependencyObject CreateShell()
        {
            var viewModel = this.Container.GetExportedValue<IShellViewModel>();
            return (System.Windows.DependencyObject)((IShellViewModel)viewModel).View;
        }

        protected override Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
        {
            var factory = base.ConfigureDefaultRegionBehaviors();
            return factory;
        }

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ChooserModel).Assembly));

        }

        protected override void InitializeShell()
        {

            regionManager.RegisterViewWithRegion("HeaderRegion", () =>
                ((IHeaderViewModel)this.Container.GetExportedValue<IHeaderViewModel>()).View);

            regionManager.RegisterViewWithRegion("FooterRegion", () =>
                ((IFooterToolbarViewModel)this.Container.GetExportedValue<IFooterToolbarViewModel>()).View);


            ((IFooterToolbarViewModel)this.Container.GetExportedValue<IFooterToolbarViewModel>()).ProjectItem = this.ProjectItem;


            ((Shell)this.Shell).ShowModal();
        } 
        #endregion

        #region "Variables"

        [Import]
        private IRegionManager regionManager = null;
        #endregion

        #region "Properties"
        public bool? IsCancel
        {
            get
            {
                return ((Shell)this.Shell).IsCancel;
            }
        }
        public ProjectItem ProjectItem { get; set; }

        #endregion

        #region "Methods"

        public ProjectItem GetCurrentItem()
        {
            var footerToolbarViewModel = (IFooterToolbarViewModel)this.Container.GetExportedValue<IFooterToolbarViewModel>();
            return footerToolbarViewModel.ProjectItem;
        }

        #endregion

        #region "Interface Implementation"

        public void Dispose()
        {
            ((Shell)this.Shell).Close();
        }
        
        #endregion
    }
}
